package bll

import (
	"sort"

	"7qule.com/sd3/CrossServer/src/bll/kuangzhan/dal"
	. "7qule.com/sd3/CrossServer/src/bll/kuangzhan/model"
	"7qule.com/sd3/CrossServer/src/bll/world"
)

// 镖局Model务逻辑类
type baseKuangzhanModelBll struct {
	// 镖局Model数据
	data map[int32]map[int32]*BaseKuangzhanModel
}

// --------------------------------初始化--------------------------------------------------

// 镖局Model逻辑类
var BaseKuangzhanModelBll = &baseKuangzhanModelBll{}

// 初始化
func init() {
	// 注册数据初始化函数
	world.LoadMgr.RegisterFunc(BaseKuangzhanModelBll.StructName(), BaseKuangzhanModelBll.initData)
}

// StructName 模块名
func (this *baseKuangzhanModelBll) StructName() string {
	return "BaseKuangzhanModelBll"
}

// initData 初始化数据
// 返回值:
// err:错误Model
func (this *baseKuangzhanModelBll) initData() (errList []error) {
	tempList, err := dal.BaseKuangzhanModelDal.GetData()
	if err != nil {
		errList = append(errList, err)
		return
	}

	tempMap := make(map[int32]map[int32]*BaseKuangzhanModel)

	for _, item := range tempList {
		if tempMap[item.LayerId] == nil {
			tempMap[item.LayerId] = make(map[int32]*BaseKuangzhanModel)
		}
		tempMap[item.LayerId][item.PositionId] = item
	}

	this.data = tempMap

	return
}

// --------------------------------生成的基础方法--------------------------------------------------

// GetData 获取数据
// 返回值:
// []*GlobalKuangzhan:获取到的镖局信息数据
func (this *baseKuangzhanModelBll) GetData() map[int32]map[int32]*BaseKuangzhanModel {
	return this.data
}

// GetItem 获取数据
// 返回值:
// *GlobalKuangzhan:获取到的镖局信息数据
func (this *baseKuangzhanModelBll) GetItem(layerId int32) (result map[int32]*BaseKuangzhanModel, exists bool) {

	result, exists = this.data[layerId]

	return
}

//
// GetSmallestItem
// @Description:返回最小节点
// @receiver this
// @return int32:最小节点
//
func (this *baseKuangzhanModelBll) GetSmallestItem() int32 {

	keys := make([]int, 0)
	for k := range this.data {
		keys = append(keys, int(k))
	}
	sort.Ints(keys)
	return int32(keys[0])
}
